﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Net;
using HtmlAgilityPack;
using System.Text.RegularExpressions;

namespace RaveEngine.Engine
{
    public abstract class BaseScrape
    {
        #region Properties

        /// <summary>
        /// The Uri of the path that is scraped and processed.
        /// </summary>
        public Uri uriSource { get; internal set; }

        /// <summary>
        /// Internal param exposing the output from the uriSource.
        /// </summary>
        public abstract string responseSource { get; }

        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="sUrl"></param>
        public BaseScrape(string sUrl)
        {
            uriSource = new Uri(sUrl);
            string r = responseSource;
        }

        #region Xslt processing 

        /// <summary>
        /// Processes the output from the url with the given xslt.
        /// </summary>
        /// <param name="xsltPath">path to the xslt to process the output from the url.</param>
        /// <returns>Processed output from the url for this item</returns>
        public string ProcessXslt(string xsltPath)
        {
            FileInfo fi = new FileInfo(xsltPath);
            if (fi.Exists)
            {
                using (FileStream fs = fi.OpenRead())
                {
                    return ProcessXslt(fs);
                }
            }
            throw new FileNotFoundException("Xslt file could not be found.  path: " + xsltPath);
        }

        /// <summary>
        /// Processes the output from the url with the given xslt.
        /// </summary>
        /// <param name="xslt">xslt to process the output from the url.</param>
        /// <returns>Processed output from the url for this item</returns>
        public string ProcessXslt(Stream xslt)
        {
            using (Stream inputStream = new MemoryStream(Encoding.UTF8.GetBytes(responseSource)))
            {
                return XsltHelper.ApplyXsltTransform(inputStream, xslt);
            }
        }

        #endregion

    }
}
